package com.aiden.cashbook

import android.app.Application
import cn.jpush.android.api.JPushInterface
import com.aiden.cashbook.bean.UseType
import com.aiden.cashbook.db.DaoHelper
import com.aiden.cashbook.extension.getBoolean
import com.aiden.cashbook.extension.putBoolean
import com.aiden.cashbook.utils.JsonUtils
import com.facebook.stetho.Stetho
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.tencent.bugly.Bugly

class CustomApplication : Application() {

    val DEBUG = BuildConfig.DEBUG

    override fun onCreate() {
        super.onCreate()
        instance = this
        DaoHelper.initDatabase(this, "cashbook.db")
        Bugly.init(this, "06a794358a", DEBUG)

        JPushInterface.setDebugMode(DEBUG)
        JPushInterface.init(this)

        Stetho.initializeWithDefaults(this)

        if (getBoolean("first_open")) {
            val usetypeJson = JsonUtils.openFile(this, "data/usetype.json")
            val useTypeList = Gson().fromJson<List<UseType>>(usetypeJson, object : TypeToken<List<UseType>>() {}.type)
            DaoHelper.getTypeDao().insertInTx(useTypeList)
            putBoolean("first_open", false)
        }
    }

    companion object {
        lateinit var instance: CustomApplication
            private set
    }

}